<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>十进制转二进制</title>
</head>
<body>
    <script>
        function Stack() {

            this.items = [];

            //  1. push(ele)
            Stack.prototype.push = function(ele) {
                this.items.push(ele)
            }

            //  2. pop()
            Stack.prototype.pop = function() {
                return this.items.pop();
            }

            //  3. peek()
            Stack.prototype.peek = function(){
                return this.items[this.items.length - 1]
            }

            //  4. isEmpty()
            Stack.prototype.isEmpty = function(){
                return this.items.length === 0
            }

            //  5. size()
            Stack.prototype.size = function() {
                return this.items.length
            }

            //  6. toString()
            Stack.prototype.toString = function(){
                let s = ''
                for(let i = 0; i < this.items.length; i++) {
                    s += this.items[i] + ' '
                }
                return s;
            }
        }
    
        function dec2bin(dec) {
            let stack = new Stack();

            while(dec > 0) {
                stack.push(dec % 2)
                dec = Math.floor(dec / 2)
            }
            
            let s = ''
            while(!stack.isEmpty()) {
                s += stack.pop()
            }
            return s;
        }

        let bin = dec2bin(100);
        console.log(bin);
        
        
    </script>
</body>
</html>